package cn.com.hhrcw.util;

import cn.hutool.core.io.IoUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @author lixingxing
 */
public class HttpUtil {
    /**
     * 发送POST请求
     * PC微博获取token + uid                  PC微博
     *
     * @param url    请求的接口路径
     * @param params 参数
     * @return
     * @throws IOException
     */
    public static String getWbTokenUid(String url, Map<String, String> params) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?client_id=");
            stringBuilder.append(params.get("client_id"));
            //第二个参数
            stringBuilder.append("&client_secret=");
            stringBuilder.append(params.get("client_secret"));
            //第三个参数
            stringBuilder.append("&grant_type=");
            stringBuilder.append(params.get("grant_type"));
            //第四个参数
            stringBuilder.append("&code=");
            stringBuilder.append(params.get("code"));
            //第五个参数
            stringBuilder.append("&redirect_uri=");
            stringBuilder.append(params.get("redirect_uri"));
            HttpPost httpPost = new HttpPost(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * GET请求
     * PC微博通过token + uid获取用户信息                PC微博
     *
     * @param url         请求的接口路径
     * @param accessToken 授权码
     * @param
     * @return
     * @throws IOException
     */
    public static String wbget(String url, Object accessToken, Object openid) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try  {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?access_token=");
            stringBuilder.append(accessToken);
            //第二个参数
            stringBuilder.append("&uid=");
            stringBuilder.append(openid);
            HttpGet httpGet = new HttpGet(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * 发送POST请求
     * PC微信获取token + openid                   PC微信
     *
     * @param url    请求的接口路径
     * @param params 参数
     * @return
     * @throws IOException
     */
    public static String getWxTokenOid(String url, Map<String, String> params) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?appid=");
            stringBuilder.append(params.get("appid"));
            //第二个参数
            stringBuilder.append("&secret=");
            stringBuilder.append(params.get("secret"));
            //第三个参数
            stringBuilder.append("&grant_type=");
            stringBuilder.append(params.get("grant_type"));
            //第四个参数
            stringBuilder.append("&code=");
            stringBuilder.append(params.get("code"));
            HttpPost httpPost = new HttpPost(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * GET请求
     * PC微信通过token + openid获取用户信息                         PC微信
     *
     * @param url         请求的接口路径
     * @param accessToken 授权码
     * @param
     * @return
     * @throws IOException
     */
    public static String wxGetUserInfo(String url, Object accessToken, Object openid) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try  {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?access_token=");
            stringBuilder.append(accessToken);
            //第二个参数
            stringBuilder.append("&openid=");
            stringBuilder.append(openid);
            HttpGet httpGet = new HttpGet(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * 发送GET请求
     * PC QQ通过code获取token                              PC QQ
     *
     * @param url    请求的接口路径
     * @param params 参数
     * @return
     * @throws IOException
     */
    public static String qqGetToken(String url, Map<String, String> params) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try  {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?grant_type=");
            stringBuilder.append(params.get("grant_type"));

            //第二个参数
            stringBuilder.append("&client_id=");
            stringBuilder.append(params.get("client_id"));

            //第三个参数
            stringBuilder.append("&client_secret=");
            stringBuilder.append(params.get("client_secret"));

            //第四个参数
            stringBuilder.append("&code=");
            stringBuilder.append(params.get("code"));

            //第五个参数
            stringBuilder.append("&redirect_uri=");
            stringBuilder.append(params.get("redirect_uri"));

            HttpGet httpGet = new HttpGet(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * GET请求
     * PCQQ通过token获取openid                                      PC QQ
     *
     * @param url         请求的接口路径
     * @param accessToken 授权码
     * @param
     * @return
     * @throws IOException
     */
    public static String qqGetOpenid(String url, Object accessToken) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try  {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?access_token=");
            stringBuilder.append(accessToken);
            stringBuilder.append("&unionid=1");
            HttpGet httpGet = new HttpGet(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

    /**
     * GET请求                                                    PC QQ
     * PCQQ通过token + openid + client_id(appid)获取用户信息
     *
     * @param url         请求的接口路径
     * @param accessToken 授权码
     * @param
     * @return
     * @throws IOException
     */
    public static String qqGetUserInfo(String url, Object accessToken, Object openid, Object oauthConsumerKey) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        try  {
            StringBuilder stringBuilder = new StringBuilder(url);
            //第一个参数
            stringBuilder.append("?access_token=");
            stringBuilder.append(accessToken);
            //第二个参数
            stringBuilder.append("&openid=");
            stringBuilder.append(openid);
            //第三个参数
            stringBuilder.append("&oauth_consumer_key=");
            stringBuilder.append(oauthConsumerKey);
            HttpGet httpGet = new HttpGet(stringBuilder.toString());
            //发送请求返回响应的信息
            CloseableHttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        }finally {
            IoUtil.close(client);
        }
        return null;
    }

}
